package com.example.hotel_demo.controller;

import com.example.hotel_demo.pojo.Room;
import com.example.hotel_demo.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@ComponentScan
public class RoomController {
    @Autowired
    private RoomService roomService;
    @Transactional
    @RequestMapping("/Admin/getRoomAll")
    @ResponseBody
    public List<Room> getRoomAll() {
        List<Room> rooms = roomService.getRoomAll();
        return rooms;
    }

    //如果传值格式是json则将改成@RequestBody，默认是使用RequestParam，所以可以改成不需要此注解
    @Transactional
    @RequestMapping("/Admin/addRoom")
    @ResponseBody
    public String addRoom(int roomID, int floor, int roomTelephone, String typeName, int roomState) {

        int flag = roomService.addRoom(roomID, floor, roomTelephone, typeName);

        if (flag == 1)
            return "addRoom添加成功";
        else
            return "添加失败";
    }
    @Transactional
    @RequestMapping("/Admin/updateRoomStateByRoomID")
    @ResponseBody
    public String updateRoomStateByRoomID(int roomState, int roomID) {
        System.out.println("updateRoomStateByRoomID");
        int flag = roomService.updateRoomStateByRoomID(roomState, roomID);
        if (flag == 1)
            return "updateRoomStateByRoomID修改成功";
        else
            return "修改失败";
    }
    @Transactional
    @RequestMapping("/Admin/updateRoomTypeByRoomID")
    @ResponseBody
    public String updateRoomTypeByRoomID(String typeName, int roomID) {
        int flag = roomService.updateRoomTypeByRoomID(typeName, roomID);
        if (flag == 1)
            return "updateRoomTypeByRoomID修改成功";
        else
            return "修改失败";
    }
    @Transactional
    @RequestMapping("/Admin/updateRoomByRoomID")
    @ResponseBody
    public String updateRoomByRoomID(int roomID, int floor, int roomTelephone, String typeName) {
        int flag = roomService.updateRoomByRoomID(roomID, floor, roomTelephone, typeName);
        if (flag == 1)
            return "updateRoomByRoomID修改成功";
        else
            return "修改失败";
    }

    @RequestMapping("/Admin/deleteRoomByRoomID")
    @ResponseBody
    public String deleteRoomByRoomID(int roomID) {

        int flag = roomService.deleteRoomByRoomID(roomID);
        if (flag == 1)
            return "deleteRoomByRoomID删除成功";
        else
            return "删除失败";
    }


    //兼容API部分，部分功能和现有API重叠,改成Restful风格

    /**
     * 获取所有房间信息 API(Restful风格)
     *
     * @return
     */
    @Transactional
    @GetMapping("/rooms")
    @ResponseBody
    @CrossOrigin
    public List<Room> getRoomList() {
        return roomService.getRoomAll();
    }

    /**
     * 获取所有房间号码 API(Restful风格)
     *
     * @return
     */
    @Transactional
    @GetMapping("/rooms/all-room-Id")
    @ResponseBody
    @CrossOrigin
    public List<Integer> getRoomIdList() {
        List<Room> roomList=roomService.getRoomAll();
        List<Integer> res=new ArrayList<>();
        for(Room room:roomList){
            res.add(room.getRoomID());
        }
        return res;
    }

    /**
     * 增加房间 API (Restful风格)
     * 返回JSON，增加成功就返回{"result":1},失败就返回{"result":-1}
     *
     * @param room
     * @return
     */
    @Transactional
    @PostMapping("/rooms")
    @ResponseBody
    @CrossOrigin
    public String addRoom(@RequestBody Room room) {
        roomService.addRoom(room.getRoomID(), room.getFloor(), room.getRoomTelephone(), room.getTypeName());
        //先直接返回成功json，后面 在补充错误逻辑
        return "{\"result\":1}";
    }

    /**
     * 删除指定房间 API (Restful风格)
     * 返回JSON，增加成功就返回{"result":1},失败就返回{"result":-1}
     *
     * @param roomID
     * @return
     */
    @Transactional
    @DeleteMapping("/rooms/{roomID}")
    @ResponseBody
    @CrossOrigin
    public String deleteRoom(@PathVariable int roomID) {
        int res=roomService.deleteRoomByRoomID(roomID);

        return "{\"result\":"+res+"}";
    }

    /**
     * 更新指定房间 API (Restful风格)
     * 返回JSON，增加成功就返回{"result":1},失败就返回{"result":-1}
     *
     * @param room
     * @return
     */
    @Transactional
    @PutMapping("/rooms")
    @ResponseBody
    @CrossOrigin
    public String updateRoom(@RequestBody Room room) {
        int res=roomService.updateRoomByRoomID(room.getRoomID(), room.getFloor(), room.getRoomTelephone(), room.getTypeName());
        return "{\"result\":"+res+"}";
    }

    /**
     * 返回房间管理页面
     *
     * @return
     */
    @GetMapping("/admin-room")
    public String adminRoom() {
        return "/admin/admin-room";
    }
}
